﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Linq;
using System.Web;
using BaoJianSoft.Common;
using BaoJianSoft.Platform.Initialization;
using BaoJianSoft.RapidWeb;
using BaoJianSoft.RapidWeb.DynamicPages;

namespace BaoJianSoft.Platform.Web.DynamicPage
{
	/// <summary>
	/// Utility to setup dynamic page temporary variables.
	/// </summary>
	public static class SetupContextTempVariablesUtility
	{
		private static IHierarchyApi hierarchyApi = SpringContext.Current.GetObject<IHierarchyApi>();

		/// <summary>
		/// Get domain from query string of current http request and save "Domain.Text" and "Domain.Value" into TempVariables of IApplicationContext.
		/// It redirects the request to PageNotFound Url if query string "domain" is invalid.
		/// </summary>
		/// <param name="e">Callback event argument.</param>
		public static void SetupOrganizationDomain(SetupApplicationContextVariablesEventArgs e)
		{
			SetupOrganizationDomain(e, true);
		}

		/// <summary>
		/// Get domain from query string of current http request and save "Domain.Text" and "Domain.Value" into TempVariables of IApplicationContext.
		/// </summary>
		/// <param name="e">Callback event argument.</param>
		/// <param name="rediectToPageNotFoundUrlIfDomainNotFound">Whether to redirect the request to PageNotFound Url if query string "domain" is invalid</param>
		public static void SetupOrganizationDomain(SetupApplicationContextVariablesEventArgs e, bool rediectToPageNotFoundUrlIfDomainNotFound)
		{
			IPlatformConfiguration platformConfiguration = SpringContext.Current.GetObject<IPlatformConfiguration>();
			string domain = HttpContext.Current.Request["Domain"];
			OrganizationDomain organizationDomain = platformConfiguration.Domains.FirstOrDefault(d => d.Value == domain);
			if (organizationDomain == null && rediectToPageNotFoundUrlIfDomainNotFound)
			{
				WebUtility.RedirectToPage(WebUtility.PageNotFoundUrl);
				return;
			}

			if (organizationDomain != null)
			{
				e.TempVariables["Domain.Text"] = organizationDomain.Text;
				e.TempVariables["Domain.Value"] = organizationDomain.Value;
			}
		}

		/// <summary>
		/// Get concrete data type from query string of current http request and save "ConcreteDataCategory" into TempVariables of IApplicationContext.
		/// </summary>
		/// <param name="e"></param>
		/// <param name="rediectToPageNotFoundUrlIfCategoryNotFound"></param>
		public static void SetupConcreteDataType(SetupApplicationContextVariablesEventArgs e, bool rediectToPageNotFoundUrlIfCategoryNotFound)
		{
			string concreteDataType = HttpContext.Current.Request["ConcreteDataType"];
			if (string.IsNullOrEmpty(concreteDataType) && rediectToPageNotFoundUrlIfCategoryNotFound)
			{
				WebUtility.RedirectToPage(WebUtility.PageNotFoundUrl);
				return;
			}

			if (!string.IsNullOrEmpty(concreteDataType))
				e.TempVariables["ConcreteDataType"] = concreteDataType;
		}

		/// <summary>
		/// Get hierarchy type from query string of current http request and save "HierarchyType" into TempVariables of IApplicationContext.
		/// </summary>
		/// <param name="e"></param>
		/// <param name="rediectToPageNotFoundUrlIfHierarchyTypeNotFound"></param>
		public static void SetupHierarchyType(SetupApplicationContextVariablesEventArgs e, bool rediectToPageNotFoundUrlIfHierarchyTypeNotFound)
		{
			IPlatformConfiguration platformConfiguration = SpringContext.Current.GetObject<IPlatformConfiguration>();
			string hierarchyType = HttpContext.Current.Request["HierarchyType"];
			if (string.IsNullOrEmpty(hierarchyType) && rediectToPageNotFoundUrlIfHierarchyTypeNotFound)
			{
				WebUtility.RedirectToPage(WebUtility.PageNotFoundUrl);
				return;
			}

			e.TempVariables["HierarchyType"] = hierarchyType;
		}
	}
}
